package com.activiti.action;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import com.activiti.biz.ActIdUserBiz;
import com.activiti.biz.BaoxiaoBiz;
import com.activiti.po.ActIdUser;
import com.activiti.po.Baoxiao;
import com.activiti.po.BaoxiaoItem;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Shenqing extends ActionSupport {
	public RuntimeService runtimeService;
	public BaoxiaoBiz baoxiaoBiz;
	public TaskService taskService;
	private String baoxiaotitle;
	private String baoxiaomemo;
	private String[] itemname;
	private String[] itemmoney;
	private String userId;
	private String taskId;
	
	public String shenqing(){
		ProcessInstance processInstance=runtimeService.startProcessInstanceByKey("Baoxiao");
		System.out.println("------流程开始------"+processInstance.getId());
		List<Task> tasks=taskService.createTaskQuery().taskCandidateGroup("User").taskName("报销申请").orderByTaskCreateTime().desc().list();
		if(tasks.size()>1){
			for(int i=1;i<tasks.size();i++)
				taskService.deleteTask(tasks.get(i).getId(), true);
		}
		taskId=tasks.get(0).getId();
		userId=(String)ActionContext.getContext().getSession().get("username");
		System.out.println(taskId);
		taskService.claim(taskId, userId);
		return SUCCESS;
	}
	
	public String baoxiao(){
		Baoxiao baoxiao=new Baoxiao();
		userId=(String)ActionContext.getContext().getSession().get("username");
		baoxiao.setActIdUser(new ActIdUser(userId));
		baoxiao.setMemo(baoxiaomemo);
		baoxiao.setTitle(baoxiaotitle);
		
		Set baoxiaoItem=new HashSet();
		for(int i=0;i<itemname.length;i++){
			BaoxiaoItem item=new BaoxiaoItem();
			item.setBaoxiao(baoxiao);
			item.setMoney(Double.valueOf(itemmoney[i]));
			item.setName(itemname[i]);
			baoxiaoItem.add(item);
		}
		baoxiao.setBaoxiaoItems(baoxiaoItem);
		baoxiaoBiz.addBaoxiao(baoxiao);
		System.out.println(taskId);
//		taskService.setVariable(taskId, "baoxiaoId", baoxiao.getId());
		Map<String, Object> variables=new HashMap<String, Object>();
		variables.put("baoxiaoId", baoxiao.getId());
		taskService.complete(taskId,variables);
		
		System.out.println("---申请提交成功,等待审核---");
		return SUCCESS;
	}
	public RuntimeService getRuntimeService() {
		return runtimeService;
	}
	public void setRuntimeService(RuntimeService runtimeService) {
		this.runtimeService = runtimeService;
	}

	public String getBaoxiaotitle() {
		return baoxiaotitle;
	}

	public void setBaoxiaotitle(String baoxiaotitle) {
		this.baoxiaotitle = baoxiaotitle;
	}

	public String getBaoxiaomemo() {
		return baoxiaomemo;
	}

	public void setBaoxiaomemo(String baoxiaomemo) {
		this.baoxiaomemo = baoxiaomemo;
	}

	public String[] getItemname() {
		return itemname;
	}

	public void setItemname(String[] itemname) {
		this.itemname = itemname;
	}

	public String[] getItemmoney() {
		return itemmoney;
	}

	public void setItemmoney(String[] itemmoney) {
		this.itemmoney = itemmoney;
	}

	public BaoxiaoBiz getBaoxiaoBiz() {
		return baoxiaoBiz;
	}

	public void setBaoxiaoBiz(BaoxiaoBiz baoxiaoBiz) {
		this.baoxiaoBiz = baoxiaoBiz;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}
	
	
}
